<?php

namespace app\admin\model;

use think\facade\Request;
use app\admin\traits\Result;
use think\Model;


class ArticleCategory extends Model
{
    //
    protected $table = 'think_article_category';
    use Result;

    //定义搜索器
    public function searchNameAttr($query,$value,$data){
        $query->where('name','like','%'.$value.'%');
    }
    public function add($data){
        //验证

        $validate = validate('ArticleCategory');
        if (!$validate->scene('add')->check($data)){
            //令牌数据无效时重置令牌
            $validate->getError() != '令牌数据无效' ? $token = Request::token() : $token = '';
            $msg = Result::error($validate->getError(), null, ['token' => $token]);
            return $msg;
        }
        $articleCategory = app('articleCategory');
        $result = $articleCategory->save($data);
        if ($result){
            $msg = Result::success('添加栏目成功',url('admin/articleCategoryList'));
        }else{
            $msg = Result::error('添加失败');
        }
        return $msg;

    }
    public function edit($data){

        //验证数据

        $validate = validate('ArticleCategory');

        if (!$validate->scene('edit')->check($data)){
            $validate->getError() != '令牌数据无效'? $token = Request::token() : $token = '';
            $msg = Result::error($validate->getError(),null,['token'=>$token]);
            return $msg;
        }
        //保存数据
        $articleCategory = app('articleCategory');
        $result = $articleCategory->save($data,['id'=>$data['id']]);
        if ($result){
            $msg = Result::success('编辑栏目成功',url('/admin/articleCategoryList'));
        }else{
            $msg = Result::error('编辑栏目失败');
        }
        return $msg;

    }
    public function del($id){
       $result =  self::where(['id'=>$id])->update(['status'=>-1]);
       if ($result !== false){
           $msg = Result::success('删除栏目成功',url('/admin/articleCategoryList'));
       }else{
           $msg = Result::error('删除栏目失败');
       }
       return $msg;
    }

    //获得所有的栏目
    public function getAllCategory(){
        $result = self::where('status','neq',-1)->select();
        return $result;
    }
}
